/*
 * Copyright (C) 2005, Velocityme Partnership
 * LoginBean.java
 *
 * Created on 09 February 2005, 09:02
 */

package com.velocityme.entity;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.UserValue;
import com.velocityme.interfaces.ServiceUnavailableException;
import java.util.Date;

/**
 *
 * @author  Robert
 * @ejb.bean
 *           type="CMP"
 *           cmp-version="2.x"
 *           name="Login"
 *           jndi-name="ejb/Login"
 *           view-type="local"
 * @ejb.transaction type="Required"
 *
 * @ejb.value-object match="*"
 *
 * @ejb.persistence table-name="login"
 *
 * @ejb.util generate="physical"
 *
 * @ejb.finder signature="java.util.Collection findAll()"
 *              query="SELECT OBJECT(o) FROM Login o"
 *
 * @ejb.finder signature="java.util.Collection findAllLoggedOn()"
 *              query="SELECT OBJECT(o) FROM Login o WHERE o.loginStatusLocal.loginStatusId = 0"
 *
 * @jboss.persistence create-table="true"
 *                    remove-table="false"
 */
public abstract class LoginBean implements EntityBean {
    
    /**
    * Context set by container
    */
    private javax.ejb.EntityContext m_entityContext;

    public void setEntityContext(javax.ejb.EntityContext entityContext) {
        m_entityContext = entityContext;
    }

    public void unsetEntityContext() {
        m_entityContext = null;
    }
  
    /** @ejb.create-method */
    public com.velocityme.interfaces.LoginPK ejbCreate(com.velocityme.interfaces.UserLocal p_userLocal,
                                                       com.velocityme.interfaces.LicenceKeyLocal p_licenceKeyLocal,
                                                       java.lang.String p_remoteHost) throws CreateException {
        try {
            setLoginId(generateNewID());
        }
        catch(ServiceUnavailableException e) {
            throw new CreateException(e.getMessage());
        }
        // EJB 2.0 spec says return null for CMP ejbCreate methods.
        return null;
    }

    public void ejbPostCreate(com.velocityme.interfaces.UserLocal p_userLocal,
                              com.velocityme.interfaces.LicenceKeyLocal p_licenceKeyLocal,
                              java.lang.String p_remoteHost) throws CreateException {
        // CMR relations must be set in ejbPostCreate
        setLoginTime(new Date());
        setLastActionTime(new Date());
        setLogoutTime(null);
        setRemoteHost(p_remoteHost);
        setUserLocal(p_userLocal);
        setLicenceKeyLocal(p_licenceKeyLocal);
        try {
            setLoginStatusLocal(LoginStatusUtil.getLocalHome().findByPrimaryKey(new LoginStatusPK(new Integer(LoginStatusBean.LOGGED_ON))));
        } catch(FinderException e) {
            throw new CreateException(e.getMessage());
        } catch(NamingException e) {
            throw new CreateException(e.getMessage());
        }
    }

    /**
     * Retrive a unique creation id to use for this bean.  This will end up
     * demarcating this bean from others when it is stored as a record
     * in the database.
     *
     * @return Returns an integer that can be used as a unique creation id.
     *
     * @throws ServiceUnavailableException Indicating that it was not possible
     *                                     to retrieve a new unqiue ID because
     *                                     the service is not available
     **/
    private Integer generateNewID() throws ServiceUnavailableException {
        int lUniqueId = -1;
        try {            
            SequenceGenerator lBean = SequenceGeneratorUtil.getHome().create();
            lUniqueId = lBean.getNextNumber("login","loginId");
            lBean.remove();
        }
        catch ( NamingException ne ) {
            throw new ServiceUnavailableException( "Naming lookup failure: " + ne.getMessage() );
        }
        catch ( CreateException ce ) {
            throw new ServiceUnavailableException( "Failure while creating a generator session bean: " + ce.getMessage() );
        }
        catch ( RemoveException re ) {
            // When the Bean cannot be removed after a while it will be taken back by the container
            // therefore ignore this exception
        }
        catch ( RemoteException rte ) {
            throw new ServiceUnavailableException( "Remote exception occured while accessing generator session bean: " +  rte.getMessage() );
        }
        
        return new Integer(lUniqueId);
    }
    
    /**
     *  This gets a local object from a remote object.
     *  @ejb.interface-method view-type="remote"
     */
    public com.velocityme.interfaces.LoginLocal getLocal() {
        return (LoginLocal)m_entityContext.getEJBLocalObject();
    }
    
    /** @ejb.interface-method view-type="local" */
    public abstract com.velocityme.valueobjects.LoginValue getLoginValue();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLoginValue(com.velocityme.valueobjects.LoginValue p_value);
    
    //=======================================================================
    //
    //                   ATTRIBUTES
    //
    //=======================================================================
      
    /**
     * @ejb.persistence column-name="loginId"
     * @ejb.interface-method view-type="local"
     * @ejb.pk-field 
     */
    public abstract java.lang.Integer getLoginId();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLoginId(java.lang.Integer loginId);

    /**
     * @ejb.persistence column-name="logintime"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.util.Date getLoginTime();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLoginTime(java.util.Date time);
    
    /**
     * @ejb.persistence column-name="lastactiontime"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.util.Date getLastActionTime();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLastActionTime(java.util.Date time);
    
    /**
     * @ejb.persistence column-name="logoutTime"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.util.Date getLogoutTime();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLogoutTime(java.util.Date logoutTime);
    
    /**
     * @ejb.persistence column-name="remoteHost"
     * @ejb.interface-method view-type="local"
     */
    public abstract java.lang.String getRemoteHost();
    /** @ejb.interface-method view-type="local" */
    public abstract void setRemoteHost(java.lang.String remoteHost);

    //=======================================================================
    //
    //                   RELATIONSHIPS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="User-Login"
     *               role-name="Login-has-a-User"
     * @jboss.relation fk-column="userIdFk"
     *                 related-pk-field="userId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.UserValue"
     *                   aggregate-name="UserValue"
     */
    public abstract com.velocityme.interfaces.UserLocal getUserLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setUserLocal(com.velocityme.interfaces.UserLocal userLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Login-LoginStatus"
     *               role-name="Login-has-a-LoginStatus"
     *               target-ejb="LoginStatus"
     *               target-role-name="LoginStatus-has-many-Logins"
     *               target-multiple="yes"
     * @jboss.relation fk-column="loginStatusIdFk"
     *                 related-pk-field="loginStatusId"
     * @ejb.value-object match="*" 
     *                   aggregate="com.velocityme.valueobjects.LoginStatusValue"
     *                   aggregate-name="LoginStatusValue"
     */
    public abstract com.velocityme.interfaces.LoginStatusLocal getLoginStatusLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLoginStatusLocal(com.velocityme.interfaces.LoginStatusLocal loginStatusLocal);
    
    /**
     * @ejb.interface-method view-type="local"
     * @ejb.relation name="Login-LicenceKey"
     *               role-name="Login-has-a-LicenceKey"
     * @jboss.relation fk-column="licenceKeyIdFk"
     *                 related-pk-field="licenceKeyId"
     */
    public abstract com.velocityme.interfaces.LicenceKeyLocal getLicenceKeyLocal();
    /** @ejb.interface-method view-type="local" */
    public abstract void setLicenceKeyLocal(com.velocityme.interfaces.LicenceKeyLocal licenceKey);
    
    //=======================================================================
    //
    //                   MANAGEMENT METHODS
    //
    //=======================================================================
    
    /**
     * @ejb.interface-method view-type="local"
     */
    public boolean isLoggedOn() {
        return getLoginStatusLocal().getLoginStatusId().intValue() == 0;
    }
}
